﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Drawing;
using System.Globalization;
using System.Text.RegularExpressions;

namespace NetJsWire.Util
{
    /// <summary>
    ///   Useful C# snippets from CambiaResearch.com
    /// </summary>
    public class ColorConverter
    {
        /// <summary>
        ///   Convert a hex string to a .NET Color object.
        /// </summary>
        /// <param name = "hexColor">a hex string: "FFFFFF", "#000000"</param>
        public static Color HexStringToColor( string hexColor )
        {
            var hc = ExtractHexDigits( hexColor );
            if ( hc.Length != 6 )
            {
                // you can choose whether to throw an exception
                //throw new ArgumentException("hexColor is not exactly 6 digits.");
                return Color.Empty;
            }
            var r = hc.Substring( 0, 2 );
            var g = hc.Substring( 2, 2 );
            var b = hc.Substring( 4, 2 );
            var color = Color.Empty;
            try
            {
                var ri
                    = Int32.Parse( r, NumberStyles.HexNumber );
                var gi
                    = Int32.Parse( g, NumberStyles.HexNumber );
                var bi
                    = Int32.Parse( b, NumberStyles.HexNumber );
                color = Color.FromArgb( ri, gi, bi );
            }
            catch
            {
                // you can choose whether to throw an exception
                //throw new ArgumentException("Conversion failed.");
                return Color.Empty;
            }
            return color;
        }

        /// <summary>
        ///   Extract only the hex digits from a string.
        /// </summary>
        public static string ExtractHexDigits( string input )
        {
            // remove any characters that are not digits (like #)
            var isHexDigit
                = new Regex( "[abcdefABCDEF\\d]+", RegexOptions.Compiled );
            var newnum = "";
            foreach ( var c in input )
            {
                if ( isHexDigit.IsMatch( c.ToString() ) )
                    newnum += c.ToString();
            }
            return newnum;
        }

        /// <summary>
        ///   Converts a Color struct to a hex string that can be used in web.
        /// </summary>
        /// <param name = "color"></param>
        /// <returns></returns>
        public static string ColorToHexString( Color color )
        {
            var ret = color.R.ToString( "X2" );
            ret += color.G.ToString( "X2" );
            ret += color.B.ToString( "X2" );

            return ret;
        }
    }
}